package news.dao;

import news.example.News;
import news.util.BaseDao;
import news.util.PageBean;

import java.util.ArrayList;
import java.util.List;

public class MainNewsPageListDaoImpl implements MainNewsPageListDao{
    private BaseDao baseDao = new BaseDao();

    @Override
    public PageBean getNewsPublishPageList(PageBean pageBean) {
        List<News> list =null;
        try {
            baseDao.openDB();
            String sql = "select * from news_publish order by id limit ?,?";
            baseDao.ps = baseDao.conn.prepareStatement(sql);
            baseDao.ps.setInt(1, pageBean.getStart());
            baseDao.ps.setInt(2,pageBean.getPageSize());
            baseDao.rs = baseDao.ps.executeQuery();

            list = new ArrayList<News>();

            while (baseDao.rs.next()){
                News news = new News();
                news.setId(baseDao.rs.getInt("id"));
                news.setTitle(baseDao.rs.getString("title"));
                news.setContent(baseDao.rs.getString("content"));
                news.setUsername(baseDao.rs.getString("username"));
                news.setDesc(baseDao.rs.getString("des"));
                news.setUserid(baseDao.rs.getInt("userid"));
                news.setCreatetime(baseDao.rs.getDate("createtime"));
                list.add(news);
            }
            pageBean.setObjectList(list);
        }catch (Exception e){
            e.printStackTrace();
        }
        return pageBean;
    }
}
